package ajaxtest;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.DefaultButtonModel;
import com.sun.org.apache.bcel.internal.generic.Select;

public class DBManager {/**/
	protected Connection connection;
	protected static PreparedStatement prst;
	
	public static Connection getConnnection() {
		Connection connection=null;
		String dbClassName ="com.mysql.jdbc.Driver"; 
		String dbUrl = "jdbc:mysql://localhost:3306/surprise"; 
		String dbUser = "root";
		String dbPwd = "123456";
		try {
			Class.forName(dbClassName);
			connection = DriverManager.getConnection(dbUrl, dbUser, dbPwd);
		} catch (Exception e) {
			System.err.println("connect失败");
			e.printStackTrace();
		}
		return connection;
	}
	
	public boolean deleteSQL(String sqlString ) {
		try {
			prst = getConnnection().prepareStatement(sqlString);
			//prst.setString(1, parameter);
			 prst.execute();
			 return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("");
			return false;
		}
	}

	public boolean deleteSQL(String sqlString ,String parameter) {
		try {
			prst = getConnnection().prepareStatement(sqlString);
			prst.setString(1, parameter);
			return prst.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("");
			return false;
		}
	}
	
	public boolean insertSQL(String sqlString ) {
		try {
			prst = getConnnection().prepareStatement(sqlString);
			prst.execute();
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("insert出错");
			return false;			
		}		
		//return true;
	}
	public ResultSet executeQuery(String sql){
		ResultSet rs=null;
		try {
			prst = getConnnection().prepareStatement(sql);
			rs=prst.executeQuery();			
		} catch (SQLException e) {
			System.err.println("com.umgsai.db.DBManager��ѯ��ݿ�ʧ�ܣ�");
			e.printStackTrace();
		}
		return rs;
	}
	
	public ResultSet executeQuery(String sql,String parameter1){
		ResultSet rs=null;
		try {
			prst = getConnnection().prepareStatement(sql);
			prst.setString(1, parameter1);
			rs=prst.executeQuery();			
		} catch (SQLException e) {
			System.err.println("com.umgsai.db.DBManager��ѯ��ݿ�ʧ�ܣ�");
			e.printStackTrace();
		}
		return rs;
	}
	
	public ResultSet executeQuery(String sql,String parameter1,String parameter2){
		ResultSet rs = null;
		try {
			prst = getConnnection().prepareStatement(sql);
			prst.setString(1, parameter1);
			prst.setString(2, parameter2);
			rs = prst.executeQuery();
		} catch (SQLException e) {
			System.err.println("com.umgsai.db.DBManager��ѯ��ݿ�ʧ�ܣ�");
			e.printStackTrace();
		}		
		return rs;
	}
	
	public boolean executeUpdate(String sql){
		try {
			prst = getConnnection().prepareStatement(sql);
			prst.execute();
		} catch (SQLException e) {
			System.err.println("com.umgsai.db.DBManager   executeUpdateʧ�ܣ�");
			e.printStackTrace();
			return false;
		}		
		return true;
	}
	
	public boolean executeUpdate(String sql,String parameter1,String parameter2){
		try {
			prst = getConnnection().prepareStatement(sql);
			prst.setString(1, parameter1);
			prst.setString(2, parameter2);
			prst.execute();
		} catch (SQLException e) {
			System.err.println("com.umgsai.db.DBManager   executeUpdateʧ�ܣ�");			
			e.printStackTrace();
			return false;
		}		
		return true;
	}
	
	public boolean executeUpdate(String sql,String parameter1,String parameter2,String parameter3){
		try {
			prst = getConnnection().prepareStatement(sql);
			prst.setString(1, parameter1);
			prst.setString(2, parameter2);
			prst.setString(3, parameter3);
			prst.execute();
		} catch (SQLException e) {
			System.err.println("com.umgsai.db.DBManager   executeUpdateʧ�ܣ�");
			
			e.printStackTrace();
			return false;
		}		
		return true;
	}
	
	public boolean executeUpdate(String sql,String parameter1,String parameter2,int parameter3){
		try {
			prst = getConnnection().prepareStatement(sql);
			prst.setString(1, parameter1);
			prst.setString(2, parameter2);
			prst.setInt(3, parameter3);
			prst.execute();
		} catch (SQLException e) {
			System.err.println("com.umgsai.db.DBManager   executeUpdateʧ�ܣ�");
			
			e.printStackTrace();
			return false;
		}		
		return true;
	}
	public boolean executeUpdate(String sql,String parameter1,String parameter2,String parameter3,String parameter4){
		try {
			prst = getConnnection().prepareStatement(sql);
			prst.setString(1, parameter1);
			prst.setString(2, parameter2);
			prst.setString(3, parameter3);
			prst.setString(4, parameter4);
			prst.execute();
		} catch (SQLException e) {
			System.err.println("com.umgsai.db.DBManager   executeUpdateʧ�ܣ�");
			
			e.printStackTrace();
			return false;
		}		
		return true;
	}
	public boolean executeUpdate(String sql,String parameter1, int parameter2,String parameter3,String parameter4){
		try {
			prst = getConnnection().prepareStatement(sql);
			prst.setString(1, parameter1);
			prst.setInt(2, parameter2);
			prst.setString(3, parameter3);
			prst.setString(4, parameter4);
			prst.execute();
		} catch (SQLException e) {
			System.err.println("com.umgsai.db.DBManager   executeUpdateʧ�ܣ�");
			
			e.printStackTrace();
			return false;
		}		
		return true;
	}
	public boolean executeUpdate(String sql,String parameter1,String parameter2,int parameter3,String parameter4,String parameter5){
		try {
			prst = getConnnection().prepareStatement(sql);
			prst.setString(1, parameter1);
			prst.setString(2, parameter2);
			prst.setInt(3, parameter3);
			prst.setString(4, parameter4);
			prst.setString(5, parameter5);
			prst.execute();
		} catch (SQLException e) {
			System.err.println("com.umgsai.db.DBManager   executeUpdateʧ�ܣ�");
			e.printStackTrace();
			return false;			
		}		
		return true;
	}
	
	public boolean executeUpdate(String sql,String parameter1,String parameter2,String parameter3,String parameter4,String parameter5){
		try {
			prst = getConnnection().prepareStatement(sql);
			prst.setString(1, parameter1);
			prst.setString(2, parameter2);
			prst.setString(3, parameter3);
			prst.setString(4, parameter4);
			prst.setString(5, parameter5);
			prst.execute();
		} catch (SQLException e) {
			System.err.println("com.umgsai.db.DBManager   executeUpdateʧ�ܣ�");
			e.printStackTrace();
			return false;			
		}		
		return true;
	}

	public boolean executeUpdate(String sql,String parameter1){
		try {
			prst = getConnnection().prepareStatement(sql);
			prst.setString(1, parameter1);
			prst.execute();
		} catch (SQLException e) {
			System.err.println("com.umgsai.db.DBManager   executeUpdateʧ�ܣ�");
			e.printStackTrace();
			return false;
		}		
		return true;
	}
	
	public void closeConnection(){
		if (prst!=null) {
			try {
				prst.close();
				System.err.println("�ر�prst��");
			} catch (SQLException e) {
				System.err.println("com.umgsai.db.DBManager�ر���ݿ�ʧ�ܣ�");
				e.printStackTrace();
			}			
		}
		if (connection!=null) {
			try {
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.err.println("�ر�connection��");
		}
	}
	
	public static void main(String[] args) {		
		DBManager dbManager = new DBManager();
	/*	ResultSet rs=dbManager.executeQuery("Select * from txyl_user where username = ? and password = ?", "umgsai","123456");
		try {
			if(rs.next()){
				System.out.println(rs.getString("password"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		dbManager.closeConnection();		
		String sql = "update txyl_user set password = '123456' where username = 'umgsai'";
		dbManager.executeUpdate(sql);
		//dbManager.closeConnection();
		*/
		
		for (int i = 1000; i < 30000; i++) {
			String sqlString ="insert into txyl_user (username,password) values('20091121"+i+"','12345')";
			//dbManager.
			dbManager.insertSQL(sqlString);
			//System.out.println("insert into txyl_user(username,password) values('201091121"+i+"','12345');");
		}
		dbManager.closeConnection();
	}


}
